/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asseco.mio.codex.controllers;

import com.asseco.mio.Assessments;
import com.asseco.mio.BasicInfo;
import com.asseco.mio.DisciplineProcedure;
import com.asseco.mio.UserInstitution;
import com.asseco.mio.WorkPosition;
import com.asseco.mio.codex.AssMark;
import com.asseco.mio.codex.AssessmentsForm;
import com.asseco.mio.codex.MeasuresTaken;
import com.asseco.mio.codex.ResponsibilityReason;
import com.asseco.mio.codex.ResponsibilityType;
import com.asseco.mio.codex.UnassessedReason;
import com.asseco.mio.core.webapp.controller.BaseFormController;
import com.asseco.mio.search.UserSearch;
import com.asseco.persistence.service.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.appfuse.model.User;
import org.appfuse.service.GenericManager;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author filip.apostolski
 */
@Controller
@RequestMapping("/searchAssessments*")
public class SearchAssessmentsController extends BaseFormController {

    private GenericManager<Assessments, String> assessmentManager = null;
    private GenericManager<BasicInfo, String> basicInfoManager = null;
    private GenericManager<WorkPosition, String> workPositionManager = null;
    private GenericManager<UnassessedReason, String> unassessedReasonManager = null;
    private GenericManager<AssMark, String> assMarkManager = null;

    @Autowired
    public void setAssessmentManager(@Qualifier("assessmentManager") GenericManager<Assessments, String> assessmentManager) {
        this.assessmentManager = assessmentManager;
    }

    @Autowired
    public void setBasicInfoManager(@Qualifier("basicInfoManager") GenericManager<BasicInfo, String> basicInfoManager) {
        this.basicInfoManager = basicInfoManager;
    }

    @Autowired
    public void setWorkPositionManager(@Qualifier("workPositionManager") GenericManager<WorkPosition, String> workPositionManager) {
        this.workPositionManager = workPositionManager;
    }

    @Autowired
    public void setUnassessedReasonManager(@Qualifier("unassessedReasonManager") GenericManager<UnassessedReason, String> unassessedReasonManager) {
        this.unassessedReasonManager = unassessedReasonManager;
    }

    @Autowired
    public void setAssMarkManager(@Qualifier("assMarkManager") GenericManager<AssMark, String> assMarkManager) {
        this.assMarkManager = assMarkManager;
    }


    public List getAssessments(Assessments assessment, User user) {
        if (assessment==null){
        return null;
    }
    if (assessment.getWorkPosition().getEmployment().getFirstName().equals("")&&assessment.getWorkPosition().getEmployment().getLastName().equals("")&&assessment.getWorkPosition().getEmployment().getSocScrNo().equals("")&&assessment.getWorkPosition().getPosition().equals("")&&assessment.getAssYear()==null&&assessment.getIsAssessed()==null&&assessment.getAssmark().getId().equals("")&&assessment.getUnassReason().getId().equals("")&&assessment.getAssHalf().equals("")){
        return null;
    }
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        String pamFirstName = "%";
        String pamLastName = "%";
        String pamSocScrNo = "%";
        String pamPosition = "%";
        String pamAssHalf = "%";
        String pamAssYear = "";
        String pamIsAssessed = "";
        String pamAssessment = "";
        String pamUnassessedReason = "";
        

        if (assessment.getWorkPosition().getEmployment().getFirstName()!=null){
            if (!assessment.getWorkPosition().getEmployment().getFirstName().equals("")){
            pamFirstName = "%" + assessment.getWorkPosition().getEmployment().getFirstName() + "%";
            }
        }
        if (assessment.getWorkPosition().getEmployment().getLastName()!=null){
            if(!assessment.getWorkPosition().getEmployment().getLastName().equals("")){
            pamLastName = "%" + assessment.getWorkPosition().getEmployment().getLastName() + "%";
            }
        }
        if (assessment.getWorkPosition().getEmployment().getSocScrNo()!=null ){
            if (!assessment.getWorkPosition().getEmployment().getSocScrNo().equals("")){
            pamSocScrNo = "%" + assessment.getWorkPosition().getEmployment().getSocScrNo() + "%";
            }
        }
        if (assessment.getWorkPosition().getPosition()!=null ){
            if (!assessment.getWorkPosition().getPosition().equals("")){
            pamPosition = "%" + assessment.getWorkPosition().getPosition() + "%";
            }
        }
        if (assessment.getAssHalf()!=null ){
            if (!assessment.getAssHalf().equals("")){
            pamAssHalf =  assessment.getAssHalf();
            }
        }
        if (assessment.getAssYear()!=null ){
            pamAssYear = " assess.assYear = "+assessment.getAssYear()+" and ";
        }
        if (assessment.getIsAssessed()!=null){
               pamIsAssessed = "assess.isAssessed ="+assessment.getIsAssessed()+" and ";
        }
        if (assessment.getAssmark()!=null) {
            if(!assessment.getAssmark().getId().equals("")) {
                AssMark x = assMarkManager.get(assessment.getAssmark().getId());
                assessment.setAssmark(x);
                pamAssessment = " assess.assmark.name like '" + assessment.getAssmark().getName() + "' and ";

            }
        }
        if (assessment.getUnassReason()!=null) {
            if(!assessment.getUnassReason().getId().equals("")) {
                UnassessedReason x = unassessedReasonManager.get(assessment.getUnassReason().getId());
                assessment.setUnassReason(x);
                pamUnassessedReason = "assess.unassReason.name like '"+ assessment.getUnassReason().getName() +"' and ";
            }
        }

        Query q = session.createQuery("from Assessments assess where "
                + "assess.workPosition.institution in (:parameter) and ( "
                + "assess.workPosition.employment.firstName like '" + pamFirstName + "' and "
                + "assess.workPosition.employment.lastName like '" + pamLastName + "' and "
                + "assess.workPosition.employment.socScrNo like '" + pamSocScrNo + "' and "
                + "assess.workPosition.position like '" + pamPosition + "' and "
                + pamAssessment
                 + pamAssYear
                +  pamIsAssessed
                + pamUnassessedReason
                + " assess.assHalf like '" + pamAssHalf + "')");

        UserInstitution ui = new UserInstitution();
        List instList = ui.getInstitutionsForUser(user);
        q.setParameterList("parameter", instList);

        List tempResult = q.list();
        List result = new ArrayList();
        for (int j = 0; j < tempResult.size(); j++) {
            Assessments object = (Assessments) tempResult.get(j);
            result.add(object);
        }
        session.getTransaction().commit();
        return result;

    }

    @ModelAttribute
    public AssessmentsForm assessmentsForm() {
         AssessmentsForm x = new AssessmentsForm(null, this.unassessedReasonManager.getAll(), assMarkManager.getAll());
         return x;
    }
   
    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView doSubmit(AssessmentsForm assessmentsForm, HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {

                String userString = request.getRemoteUser();
        User user = UserSearch.getUserByUserName(userString);

        AssessmentsForm x = this.assessmentsForm();
            if(assessmentsForm.getAssessment()!=null){
                x.setAssessment(assessmentsForm.getAssessment());
            }
            else{
                x.setAssessment(null);
            }
        return new ModelAndView("searchAssessments", "searchAssessmentsList", getAssessments(x.getAssessment(), user));
    }
    @RequestMapping(method = RequestMethod.GET)
    protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response)
            throws Exception {


         String userString2 = request.getRemoteUser();
            if (userString2 != null) {
                FirstLoginCheck x = new FirstLoginCheck();

                if (x.isFirstLogin(userString2)) {
                    return new ModelAndView("/firstlogin");
                }
            }
          return new ModelAndView("searchAssessments", "searchAssessmentsList", null);
    }
}
